<template>
  <div :class="{ hidden: hidden }" class="pagination-container">
    <el-pagination
      :background="background"
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :layout="layout"
      :page-sizes="pageSizes"
      :pager-count="pagerCount"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </div>
</template>

<script setup>
import { scrollTo } from "@/utils/scroll-to";
const props = defineProps({
  total: {
    required: true,
    type: Number,
  },
  page: {
    type: Number,
    default: 1,
  },
  limit: {
    type: Number,
    default: 20,
  },
  pageSizes: {
    type: Array,
    default() {
      return [10, 20, 30, 50];
    },
  },
  // 移动端页码按钮的数量端默认值5
  pagerCount: {
    type: Number,
    default: document.body.clientWidth < 992 ? 5 : 7,
  },
  layout: {
    type: String,
    // default: 'total, sizes, prev, pager, next, jumper'
    default: "total,  prev, pager, next ",
  },
  background: {
    type: Boolean,
    default: false,
  },
  autoScroll: {
    type: Boolean,
    default: true,
  },
  hidden: {
    type: Boolean,
    default: false,
  },
});

const emit = defineEmits();
const currentPage = computed({
  get() {
    return props.page;
  },
  set(val) {
    emit("update:page", val);
  },
});
const pageSize = computed({
  get() {
    return props.limit;
  },
  set(val) {
    emit("update:limit", val);
  },
});
function handleSizeChange(val) {
  if (currentPage.value * val > props.total) {
    currentPage.value = 1;
  }
  emit("pagination", { page: currentPage.value, limit: val });
  if (props.autoScroll) {
    scrollTo(0, 800);
  }
}
function handleCurrentChange(val) {
  emit("pagination", { page: val, limit: pageSize.value });
  if (props.autoScroll) {
    scrollTo(0, 800);
  }
}
</script>

<style scoped lang="scss">
.pagination-container.hidden {
  display: none;
}
:deep(.el-pagination) {
  button.btn-prev {
    background-color: #356df2 !important;
    border-radius: 16px 0 0 16px;
  }
  button.btn-next {
    background-color: #356df2 !important;
    border-radius: 0 16px 16px 0;
  }
  button {
    color: rgb(226, 226, 226);
    &:hover {
      color: white;
    }
  }
}
:deep(.el-pager) {
  z-index: 1;
  .btn-prev {
    background-color: #356df2 !important;
  }
  .number {
    cursor: pointer;
    background-color: #356df2 !important;
    color: white;
    &.is-active {
      background-color: white !important;
      color: #356df2;
      border-radius: 50% !important;
      width: 24px !important;
      min-width: 24px;
      margin: 4px;
      height: 24px;
      // border: 1px solid red;
      position: relative;

      &::after {
        content: "";
        position: absolute;
        height: 32px;
        width: 33px !important;
        background-color: #356df2;
        transform: translate(-5px, -4px);
        left: 0;
        top: 0;
        z-index: -1 !important;
      }
    }
  }
}
  :deep(.el-icon-arrow-right:before) {
    content: '';//右边“下一页”三角箭头
  }
.el-icon-arrow-left:before {
    content: '';//左边“上一页”三角剪头
  }
</style>
